// @Title 请填写文件名称
// @Description 请填写文件描述
// @Author 赵伟 2022/4/12 10:18

package v1

import (
    "authserver/global"
    "authserver/global/response"
    "authserver/model"
    "authserver/service"
    "authserver/utils"
    "fmt"
    "github.com/gin-gonic/gin"
    "io"
    "net/http"
    "strings"
)

// RegAuth @Tags API
// @Summary 设备注册
// @Router /api/regAuth [post]
func RegAuth(c *gin.Context) {

	key := c.Query("key")
	token := c.Query("auth")

	if key == "" {
		global.GVA_LOG.Error("请输入注册码....")
		response.OkWithData("", c)
		return
	}

	err, authInfo := service.RegAuth(key, token)
	authInfo.Status = 0
	if err != nil {
		authInfo.Status = 1001
	}

	enKey := utils.MD5V([]byte(key))
	enKey = enKey[:16]
	keyBs := []byte(enKey)
	enStr := utils.AesEncryptNormal([]byte(authInfo.JsonStr()), keyBs)
	response.OkWithData(enStr, c)
}

// ParseDy @Tags API
// @Summary 抖音视频解析
// @Router /api/parseDy [post]
func ParseDy(c *gin.Context) {

    var (
        err error
        videoInfo model.VideoResp
    )

	key := c.Query("key")
	videoUrl := c.Query("url")



	if key == "" {
		global.GVA_LOG.Error("请输入注册码....")
		response.FailWithMessage("请输入注册码", c)
		return
	}

    if strings.Contains(videoUrl,"douyin.com"){
        //err, videoInfo = service.DyUrlParse(videoUrl,key)
        err, videoInfo = service.DyUrlParse3(videoUrl,key)
    }

    if strings.Contains(videoUrl,"kuaishou.com"){
        err, videoInfo = service.KsUrlParse(videoUrl,key)
    }

	if err != nil {
        global.GVA_LOG.Error(err.Error())
		response.FailWithMessage("解析失败",c)
		return
	}

	response.OkWithData(videoInfo,c)
}

// DyTest
// @Summary 抖音网站初始化
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ApiBody true "保存商城销售单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/dyTest [post]
func DyTest(c *gin.Context)  {

    service.ChromeTest("https://www.douyin.com/")
    response.OkWithMessage("ok",c)
}


// CheckUpdate @Tags API
// @Summary 保存商城销售单
// @Router /api/checkUpdate [post]
func CheckUpdate(c *gin.Context) {

	key := c.Query("key")

	if key == "" {
		global.GVA_LOG.Error("请输入注册码....")
		response.FailWithMessage("请输入注册码", c)
		return
	}

	err, urlInfo := service.CheckUpdate(key)
	if err != nil {
        global.GVA_LOG.Error(err.Error())
		response.FailWithMessage("解析失败",c)
		return
	}

	response.OkWithData(urlInfo,c)
}

// ParseKs @Tags API
// @Summary 快手视频去水印接口
// @Router /api/parseKs [post]
func ParseKs(c *gin.Context) {

    key := c.Query("key")
    videoUrl := c.Query("url")

    if key == "" {
        global.GVA_LOG.Error("请输入注册码....")
        response.FailWithMessage("请输入注册码", c)
        return
    }

    err, urlInfo := service.KsUrlParse(videoUrl,key)
    if err != nil {
        global.GVA_LOG.Error(err.Error())
        response.FailWithMessage("解析失败",c)
        return
    }

    response.OkWithData(urlInfo,c)
}

// AppletTouristLogin @Tags 小程序——基本接口
// @Summary 游客登录
// @Router /api/touristLogin [post]
func AppletTouristLogin(c *gin.Context) {
    var param model.AppletTouristLogin
    _ = c.ShouldBindJSON(&param)

    if param.Code == "" {
        response.FailWithMessage("缺少Code", c)
        return
    }

    appId:=c.GetHeader("AppId")

    err, member := service.AppletTouristLogin(appId,param)
    if err != nil {
        response.FailWithMessage(err.Error(), c)
    } else {
        response.OkWithData(member,c)
    }
}

// DownloadVideo @Tags 小程序——基本接口
// @Summary 下载视频
// @Router /api/downloadVideo [post]
func DownloadVideo(c *gin.Context) {


    key := c.Query("key")

    if key == "" {
        response.FailWithMessage("下载地址", c)
        return
    }

    err, reader,length,cType := service.DownloadUrlProxy(key)
    if err != nil {
        response.FailWithMessage(err.Error(), c)
    } else {

        c.Header("Content-Type", cType)
        c.Header("Content-Disposition", "attachment; filename="+key+".mp4")
        c.Header("Content-Transfer-Encoding", "binary")
        c.Header("Content-length", fmt.Sprintf("%d",length))
        _,err=io.Copy(c.Writer,reader)
    }
}

// GetShortRul @Tags 生成短连接
// @Summary 生成短连接
// @Router /api/shortUrl [post]
func GetShortRul(c *gin.Context)  {

    var param model.ShortUrl
    _ = c.ShouldBindJSON(&param)

    if param.Username == "" {
        global.GVA_LOG.Error("请输入注册码....")
        response.FailWithMessage("请输入注册码", c)
        return
    }
    if param.RealUrl==""{
        response.FailWithMessage("缺少源视频地址", c)
        return
    }
    err,shortUrl:=service.AddShortUrl(param.Username,param.RealUrl)
    if err!=nil{
        response.FailWithMessage("获取失败", c)
        return
    }
    response.OkWithData(shortUrl,c)
    return
}

// VisitShortUrl
// @Summary 访问短连接
// @Router /st/:shortKey [GET]
func VisitShortUrl(c *gin.Context)  {
    shortKey:=c.Param("shortKey")

     realUrl:= service.GetShortUrlByKey(shortKey)

     c.Redirect(http.StatusFound,realUrl)

}
